<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="param">
                <el-form-item>
                    <el-input v-model="param.keyword" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="searchkeyword">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="listdata" highlight-current-row v-loading="false" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="id" label="id" width="120" v-if="false">
            </el-table-column>
            <el-table-column prop="sn" label="部门编号" width="180" >
            </el-table-column>
            <el-table-column prop="name" label="部门名称" width="180" >
            </el-table-column>
            <el-table-column prop="state" label="状态" width="180" :formatter="formatSex" >
            </el-table-column>
            <el-table-column prop="manager.username" label="部门管理员" width="180" >
            </el-table-column>
            <el-table-column prop="parent.name" label="上级部门" width="180" >
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="param.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>


        <!--新增\编辑界面-->
        <el-dialog title="操作" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="部门编号" prop="sn">
                    <el-input v-model="addForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">启用</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="管理员">
                    <el-select v-model="addForm.manager" value-key="id" placeholder="请选择">
                        <el-option
                                v-for="emp in emps"
                                :key="emp.id"
                                :label="emp.username"
                                :value="emp" >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="上级部门">
                    <el-select v-model="addForm.parent" value-key="id" placeholder="请选择">
                        <el-option
                                v-for="dep in deps"
                                :key="dep.id"
                                :label="dep.name"
                                :value="dep"
                                :disabled="dep.name==addForm.name?true:false">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="false">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    export default {
        name: "dept",
        data(){
            return {
                // 分页+高级查询参数
                param:{
                    currentPage:1,
                    pageSize:5,
                    keyword: ""
                },
                // 总条数
                total:0,
                // 分页+高级查询所得数据
                listdata:{},
                sels:[],
                // 对话框是否显示
                addFormVisible:false,
                // 对话框里面的数据
                addForm:{
                    sn:'',
                    name:'',
                    state:1,
                    manager:[],
                    parent:[]
                },
                // 对话框里面的数据规则（必填）
                addFormRules: {
                    name: [
                        { required: true, message: '请输入部门名称', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入部门编号', trigger: 'blur' }
                    ]
                },
                emps:[],
                deps:[]
            }
        },
        // 所有方法
        methods:{
            // 提交数据
            addSubmit(){
                //console.log(this.addForm);
                this.$http.put("/dep",this.addForm).then(res=>{
                    this.addFormVisible=false;
                    this.loadlistData();
                    if(res.data.success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else{
                        this.$message({
                            message: '网络异常',
                            type: 'warning'
                        });
                    }
                });
            },
            // 高级查询
            searchkeyword(){
                 this.param. currentPage=1;
                 this.loadlistData();
            },
            //新增弹出框
            handleAdd(){
                this.loadEmpData();
                this.loadDepData();
                this.addForm={state:1};
                this.addFormVisible=true;
                //console.log(this.addForm);
            },
            // 当勾选选项的时候出发函数
            selsChange(val){
                this.sels=val;
            },
            // 由于启用，禁用的值与我们想展示的值不同
            //状态的显示转换
            formatSex(row, column) {
                return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
            },
            //点击编辑触发函数
            handleEdit(i,r){
                this.loadEmpData();
                this.loadDepData();
                this.addFormVisible=true;
                this.addForm=Object.assign({},r);
                // this.emps.forEach(function(value,index,array){
                //    console.log(value);
                // });
                // this.deps.forEach(function(value,index,array){
                //     if(value.name==this.addForm.name){
                //         console.log(value);
                //         value.disabled = true;
                //     }
                // });

            },
            //点击删除触发函数
            handleDel(i,r){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { id: r.id };
                    this.$http.delete("/dep/"+para.id).then(res=>{
                        this.listLoading = false;
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                        this.loadlistData();
                    });
                }).catch(() => {
                    this.$message('已取消删除');
                });
            },
            // 批量删除
            batchRemove(){
                // 拿到勾选的id数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    this.$http.patch("/dep",ids).then(res=>{
                        this.listLoading = false;
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                        this.loadlistData();
                    })
                }).catch(() => {
                    this.$message('已取消删除');
                });

            },
            //点击翻页时触发函数
            handleCurrentChange(v){
                this.param.currentPage = v;
                this.loadlistData();
            },
            loadlistData(){
                // 发送请求，从后端拿数据
                this.$http.post("/dep",this.param).then(res=>{
                    //console.log(res);
                    let {data,total}=res.data;
                    this.listdata=data;
                    this.total=total;
                })
            },
            loadEmpData(){
                // 发送请求，从后端拿数据
                this.$http.get("/emp").then(res=>{
                    //console.log(res);
                    this.emps=res.data;
                })
            },
            loadDepData(){
                // 发送请求，从后端拿数据
                this.$http.get("/dep").then(res=>{
                    //console.log(res);
                    this.deps=res.data;
                })
            }
        },
        // 钩子函数
        mounted(){
            this.loadlistData();
        }
    }
</script>

<style scoped>

</style>